package com.qtt.app.service.impl;

import com.qtt.app.entity.AppComment;
import com.qtt.app.entity.Comment;
import com.qtt.app.entity.User;
import com.qtt.app.entity.ReturnInfo.ReturnCourseComments;
import com.qtt.app.mapper.AppCommentMapper;
import com.qtt.app.mapper.CommentMapper;
import com.qtt.app.mapper.UserMapper;
import com.qtt.app.service.AppCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.qtt.app.utils.Converter.getAsFloat;

@Service
public class AppCommentServiceImpl implements AppCommentService {
    @Autowired
    AppCommentMapper appCommentMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    UserMapper userMapper;

//    //查看一门课的所有评论
//    @Override
//    public List<ReturnCourseComments> getCourseCommentList(String courseId) {
//        List<ReturnCourseComments> returnCourseCommentsEntities = new ArrayList<ReturnCourseComments>();
//        ReturnCourseComments returnCourseCommentsEntity = new ReturnCourseComments();
//        //从app comment表中获取到该课程的所有评论列表，之后再遍历
//        List<AppComment> appCommentEntites = appCommentMapper.getCourseCommentList(courseId);
//        for(int i=0;i<appCommentEntites.size();i++) {
//            //遍历，赋值单个的评论实体，接下来就处理这个实体
//            AppComment appCommentEntity = appCommentEntites.get(i);
//            returnCourseCommentsEntity.setGrade(appCommentEntity.getGrade());
//            Integer commentId = appCommentEntity.getCommentId();
//            //拿到app_comment表中拿到commentId，去comment表中获取其他数据
//            Comment commentEntity = commentMapper.selectByPrimaryKey(commentId);
//            returnCourseCommentsEntity.setCommentId(commentId);
//            returnCourseCommentsEntity.setUserId(commentEntity.getUserId());
//            returnCourseCommentsEntity.setContent(commentEntity.getContent());
//            returnCourseCommentsEntity.setTime(commentEntity.getTime());
//
//
//            //拿到app_comment表中的userId,去用户表中获取用户数据
//            Long userId = appCommentEntity.getUserId();
//            returnCourseCommentsEntity.setUserId(userId);
//            User userEntity = userMapper.getById(userId);
//            returnCourseCommentsEntity.setNickname(userEntity.getNickname());
//            returnCourseCommentsEntity.setUserPicture(userEntity.getHead());
//            returnCourseCommentsEntity.setCompany(userEntity.getCompany());
//            returnCourseCommentsEntity.setPosition(userEntity.getPosition());
//
//            //将单个评论加入返回的列表中去
//            returnCourseCommentsEntities.add(returnCourseCommentsEntity);
//        }
//        return returnCourseCommentsEntities;
//    }

    //查看一门课的所有评论
    @Override
    public List<ReturnCourseComments> getCourseCommentList(String generalId) {
        List<ReturnCourseComments> returnCourseCommentsEntities = new ArrayList<ReturnCourseComments>();

        //从comment表中获取到该课程的所有评论列表，之后再遍历
        List<Comment> commentEntites = commentMapper.getCommentsByGeneralId(generalId);
        for(int i=0;i<commentEntites.size();i++) {
            //遍历，赋值单个的评论实体，接下来就处理这个实体
            Comment commentEntity = commentEntites.get(i);
            ReturnCourseComments returnCourseCommentsEntity = new ReturnCourseComments();
            returnCourseCommentsEntity.setCommentId(commentEntity.getId());
            returnCourseCommentsEntity.setUserId(commentEntity.getUserId());
            returnCourseCommentsEntity.setContent(commentEntity.getContent());
            returnCourseCommentsEntity.setTime(commentEntity.getTime());

            //拿到comment表中的userId,去用户表中获取用户数据
            Long userId = commentEntity.getUserId();
            User userEntity = userMapper.getById(userId);
            returnCourseCommentsEntity.setNickname(userEntity.getNickname());
            returnCourseCommentsEntity.setUserPicture(userEntity.getHead());
            returnCourseCommentsEntity.setCompany(userEntity.getCompany());
            returnCourseCommentsEntity.setPosition(userEntity.getPosition());

            //随机返回分数
            Float grade =getAsFloat(((Math.random()*(49-35+1))+35)/10);
            BigDecimal b = new  BigDecimal(grade);
            float  grade2   =  b.setScale(2,  BigDecimal.ROUND_HALF_UP).floatValue();
            returnCourseCommentsEntity.setGrade(grade2);

            //将单个评论加入返回的列表中去
            returnCourseCommentsEntities.add(returnCourseCommentsEntity);
        }
        return returnCourseCommentsEntities;
    }

    //添加评论，需要结合orderFormService进行操作。拿到它的数据。
    @Override
    public int addComment(String courseId, Long userId, String content, Float grade, String orderId) {
        Comment commentEntity = new Comment();
        commentEntity.setCourseId(courseId);
        commentEntity.setUserId(userId);
        commentEntity.setContent(content);
        commentEntity.setTime(new Date());
        commentEntity.setStatus(1);
        int b = commentMapper.insert(commentEntity);
        if(b == 0){
            return 400;//创建失败
        }
        else{
            int lastCommentId = commentMapper.getLastId();//unreachable statement异常：因为在return后面写语句
            System.out.println("lastId="+lastCommentId);
            AppComment appCommentEntity = new AppComment();
            appCommentEntity.setCommentId(lastCommentId);
            appCommentEntity.setUserId(userId);
            appCommentEntity.setCourseId(courseId);
            appCommentEntity.setGrade(grade);
            appCommentEntity.setOrderId(orderId);
            int createFlag2 = appCommentMapper.insert(appCommentEntity);
            if(createFlag2==0){return 400;}
            else{return 200;}

        }


    }

    @Override
    public AppComment getById(Long appCommentId) {
        AppComment appCommentEntity = appCommentMapper.selectByPrimaryKey(appCommentId);
        return appCommentEntity;
    }

    @Override
    public int updateContentOrGrade(Long id, Float grade, String content) {
        AppComment appCommentEntity = appCommentMapper.selectByPrimaryKey(id);
        if (appCommentEntity ==null ){
            return 401;// 表示根本不存在该评论，即评论id输错了
        }
        if(grade==null && content == null){return 404;}//表示grade和content都是空
        if(grade!=null){
            appCommentEntity.setGrade(grade);
            int flag1 =appCommentMapper.updateByPrimaryKey(appCommentEntity);
            if(flag1 == 0){return 402;}//表示修改content时出错
        }
        if(content != null){
            Integer commentId = appCommentEntity.getCommentId();
            Comment commentEntity = commentMapper.selectByPrimaryKey(commentId);
            commentEntity.setContent(content);
            int flag2 = commentMapper.updateByPrimaryKeyWithBLOBs(commentEntity);
            if(flag2 == 0){return 403;}//表示修改grade时出错
        }
        return 200;//其他情况一律返回200，表示成功
    }


}
